/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2007
*
*****************************************************************************/

#ifndef _SERIAL_PORT_API_H
#define _SERIAL_PORT_API_H

#include "gui.h"
#include <tchar.h>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <Mmsystem.h>
using namespace std;


//GPS Channels definitions
#define NUM_CH 20
#define PSEUDO_CH 32

//GPS relate constants
#define Knot2Kmhr 1.8532

//GPS FixType
#define NoFix 0
#define SPS 1
#define DGPS 2
#define Estimate 6



typedef struct SVInfo
{
    int SVid;            // PRN
    int SNR;
    int elv;             // elevation angle : 0~90
    int azimuth;         // azimuth : 0~360
    unsigned char Fix;   // 0:None , 1:FixSV
} SVInfo;

typedef struct ChInfo
{
    int SVid;            // PRN
    int SNR;             // SNR
    unsigned char Status;// Status(0:Idle, 1:Search, 2:Tracking)
} ChInfo;

typedef struct Version
{
    char MCore[30];      // MCore Version
    char Customer[30];   // Customer Version
} Version;

typedef struct GPSInfo
{
	int year;
	int mon;
    int day;
    int hour;
    int min;
    float sec;

    float Lat; // Position, +:E,N -:W,S
    float Lon;
    float Alt;
    unsigned char FixService;  // NoFix:0, SPS:1, DGPS:2, Estimate:6
    unsigned char FixType;     // None:0, 2D:1, 3D:2
    float Speed;  // km/hr
    float Track;  // 0~360
    float PDOP;   //DOP
    float HDOP;
    float VDOP;

    int SV_cnt;
    int fixSV[NUM_CH];
}GPSInfo;

typedef struct MsgMapping
{
    HWND        hWndReg;
    UINT        MsgReg;
}MsgMapping;

void InitialGPSData(void);

BOOL OpenSerialPort(wstring ComPort,wstring Baudrate);
void CreateComRecThread(void);
void TerminateComRecThread();
void CloseSerialPort();

BOOL StartLogNema(const char* fpath);
void StopLogNema();

BOOL WriteCmd(const char* Cmd);
int  SVidSort( const void *a, const void *b);
void GenerateCkSum(char *buf, char *result);
bool GenerateFullCmd(char *buf, UINT len);

BOOL RegisterMsg(HWND hWndView, UINT nMsg);
BOOL WCharToMByte(LPCWSTR lpcwszStr, LPSTR lpszStr, DWORD dwSize);

extern GPSInfo g_gpsInfo;
extern SVInfo  g_svInfo[NUM_CH];
extern ChInfo  g_chInfo[PSEUDO_CH];
extern wstring g_strFirmwareVer;

extern bool g_fgSVUpdate;
extern bool g_fgChUpdate;
extern wstring g_strNMEALog;

#endif 